package ${package.Controller};


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import ${package.Entity}.${entity};
import ${package.Entity}.param.${entity}QueryParam;
import ${package.Entity}.param.${entity}UpdateParam;
import ${package.Entity}.param.${entity}AddParam;
import ${package.Service}.${entity}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
@Validated
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
@Api(value = "${cfg.mchname}", tags = {"${table.comment!}管理"})
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
@Api(value = "${cfg.mchname}", tags = {"${table.comment!}管理"})
public class ${table.controllerName} {
</#if>
    @Autowired
    private ${entity}Service ${entity?uncap_first}Service;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询${table.comment!}
    */
    @ApiOperation(value = "分页查询${table.comment!}")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid ${entity}QueryParam param) {
        try {
            log.info("分页查询${table.comment!}");
            QueryWrapper<${entity}> queryWrapper = new QueryWrapper<${entity}>();
            <#list table.fields as field>
                <#assign getprefix="get"/>
                <#if field.propertyName=='updatedtime'||field.propertyName=='createdby'||field.propertyName=='createdtime'||field.propertyName=='updatedby'||field.propertyName=='updateby'||field.propertyName=='modifiedby'||field.propertyName=='modifiedtime'>
                <#else>
                <#if field.propertyType=="String">
            queryWrapper.eq(StringUtils.isNotBlank(param.${getprefix}${field.propertyName?cap_first}()), "${field.name}", param.${getprefix}${field.propertyName?cap_first}());
                <#else>
            queryWrapper.eq(param.${getprefix}${field.propertyName?cap_first}() != null, "${field.name}", param.${getprefix}${field.propertyName?cap_first}());
                </#if>
                </#if>
            </#list>
            IPage<${entity}> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(${entity}.class));
            return Response.data(${entity?uncap_first}Service.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询${table.comment!}异常", e);
            return Response.failed("分页查询${table.comment!}异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询${table.comment!}
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询${table.comment!}")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(${entity?uncap_first}Service.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增${table.comment!}")
    public Response save(@RequestBody @Valid ${entity}AddParam param) {
        try {
            ${entity} data = BeanUtils.covertType(param, ${entity}.class);
            return Response.data(${entity?uncap_first}Service.save(data));
        } catch (ServiceException e) {
            log.error("新增${table.comment!}异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增${table.comment!}异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改${table.comment!}
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改${table.comment!}")
    public Response update(@RequestBody @Valid ${entity}UpdateParam param) {
        try {
            ${entity} data = BeanUtils.covertType(param, ${entity}.class);
            return Response.data(${entity?uncap_first}Service.updateById(data));
        }catch (ServiceException e) {
            log.error("修改${table.comment!}异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改${table.comment!}异常", e);
            return Response.failed();
        }
    }
}
</#if>
